package com.flink.split_stream.union;

import com.flink.entity.User;
import com.flink.window.WindowFunctionDemo;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.time.LocalDate;

/**
 * 描述:
 * 合流操作-联合
 *
 * @author yanzhengwu
 * @create 2022-08-14 23:48
 */
public class UnionStream {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<User> stream1 = env.socketTextStream("192.168.22.111", 8888)
                .map(data -> {
                    String[] strings = data.split(",");
                    return new User(strings[0].trim(), strings[1].trim(), LocalDate.now(), 1, Long.valueOf(strings[2].trim()));
                })
                .assignTimestampsAndWatermarks(WatermarkStrategy.<User>forBoundedOutOfOrderness(Duration.ofSeconds(2))
                        .withTimestampAssigner(new SerializableTimestampAssigner<User>() {
                            @Override
                            public long extractTimestamp(User element, long recordTimestamp) {
                                return element.getTimestamp();
                            }
                        }));
        stream1.print("stream1");

        SingleOutputStreamOperator<User> stream2 = env.socketTextStream("192.168.22.112", 8888)
                .map(data -> {
                    String[] strings = data.split(",");
                    return new User(strings[0].trim(), strings[1].trim(), LocalDate.now(), 1, Long.valueOf(strings[2].trim()));
                })
                .assignTimestampsAndWatermarks(WatermarkStrategy.<User>forBoundedOutOfOrderness(Duration.ofSeconds(5))
                        .withTimestampAssigner(new SerializableTimestampAssigner<User>() {
                            @Override
                            public long extractTimestamp(User element, long recordTimestamp) {
                                return element.getTimestamp();
                            }
                        }));
        stream2.print("stream2");
        //合流操作（联合）
        stream1.union(stream2)
                .process(new ProcessFunction<User, String>() {

                    @Override
                    public void processElement(User value, Context ctx, Collector<String> out) throws Exception {
                        out.collect("水位线：" + ctx.timerService().currentWatermark());
                    }
                }).print();
        //TODO 合流操作由于两个流的等待时间和水位线的不同，此处合流是以所有合并的数据流中最小的水位线为基准！
        //TODO 合流操作时 是以广播的方式将所有的watermark传递给不同的流，告诉其他流中的水位线我到了多少

        env.execute();

    }
}
